package cn.itcast.web.controller.cargo;

import cn.itcast.service.cargo.ContractService;
import cn.itcast.utils.DownloadUtil;
import cn.itcast.vo.ContractProductVo;
import cn.itcast.web.controller.BaseController;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.data.annotation.Reference;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.List;

@Controller
@RequestMapping("/cargo/contract/")
public class ExcelController extends BaseController {

    @Reference
    private ContractService contractService;

    //进入下载的页面
    @RequestMapping("/print")
    public String print() {
        return "cargo/print/contract-print";
    }

    /**
     * 出货表打印
     * 参数 ：inputDate（船期）
     * 业务：
     * 1、根据船期查询数据
     * 2、构造excel文件
     * 3、下载excel
     * 下载的时候，不需要返回值，内部是通过输出流的形式下载文件
     */
    @RequestMapping("/printExcel")
    public void printExcel(String inputDate) throws IOException {
//i、根据船期查询数据
        List<ContractProductVo> list = contractService.findByShipTime(inputDate+"%");
        //ii、构造excel文件
        //1.创建工作簿
        Workbook wb = new XSSFWorkbook();
        //2.创建第一页
        Sheet sheet = wb.createSheet("Sheet1");
        //3.构造大标题
        //3.1 创建第一行
        Row row = sheet.createRow(0);
        //3.2 创建第二个单元格
        Cell cell = row.createCell(1);
        //3.3 设置单元格内容
        //  2020-11--> 2020-11   -->2020年11              月份出货表
        //  2020-01--> 2020-1 -->2020年1
        inputDate = inputDate.replaceAll("-0","-").replaceAll("-","年");
        cell.setCellValue(inputDate + "月份出货表");
        //4.构造小标题
        //4.1 创建第二行
        row = sheet.createRow(1);
        String [] titles = new String[]{"","客户","订单号","货号","数量","工厂","工厂交期","船期","贸易条款" };
        for (int i = 0; i < titles.length; i++) {
            String title = titles[i];
            //4.2 创建每一个单元格
            cell = row.createCell(i);
            //4.3 设置单元格的内容
            cell.setCellValue(title);
        }
        //5.循环数据列表，构造数据行内容
        int index = 2;
        for (ContractProductVo cp : list) {
            row = sheet.createRow(index);

            cell = row.createCell(1);
            cell.setCellValue(cp.getCustomName());

            cell = row.createCell(2);
            cell.setCellValue(cp.getContractNo());

            cell = row.createCell(3);
            cell.setCellValue(cp.getProductNo());

            cell = row.createCell(4);
            cell.setCellValue(cp.getCnumber());

            cell = row.createCell(5);
            cell.setCellValue(cp.getFactoryName());

            cell = row.createCell(6);
            cell.setCellValue(cp.getDeliveryPeriod());

            cell = row.createCell(7);
            cell.setCellValue(cp.getShipTime());

            cell = row.createCell(8);
            cell.setCellValue(cp.getTradeTerms());

            index ++;
        }
        //iii、文件下载
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        wb.write(bos);
        new DownloadUtil().download(bos,response,"出货表.xlsx"); //ByteArrayOutputStream ,response，下载文件名
    }
}
